<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入姓名" suffix-icon="el-icon-search" v-model="realName"></el-input>
      <el-input class="ml-5" style="width: 200px" placeholder="请输入手机号" suffix-icon="el-icon-search"
                v-model="phone"></el-input>
      <el-button class="ml-5" type="primary" @click="query">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="gander" label="性别" width="55">
        <template slot-scope="scope">
          <el-tag :type="scope.row.gender === 1 ? 'primary' : 'danger' ">{{ scope.row.gender === 1 ? '男' : '女' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="250" align="center"></el-table-column>
      <el-table-column prop="phone" label="手机号" width="150" align="center"></el-table-column>
      <el-table-column prop="realName" label="姓名" width="200" align="center"></el-table-column>
      <el-table-column prop="branchName" label="单位" width="150" align="center"></el-table-column>
      <el-table-column label="是否进行排班" width="150" align="center">
        <template slot-scope="scope">
          <el-switch active-color="#13ce66" inactive-color="#ff4949" @change="personStatusChangeHandler(scope.row)"
                     v-model="scope.row.enable"></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="400" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="shiftHistoryDialogOpenHandler(scope.row)">值班记录 <i class="el-icon-date"></i>
          </el-button>
          <el-button type="warning" @click="statisticsDialogOpenHandler(scope.row)">值班统计 <i class="el-icon-s-data"></i>
          </el-button>
          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="form.realName" :visible.sync="dialogFormVisible" width="40%" :close-on-click-modal="false">
      <el-form label-width="120px" size="small" style="width: 80%; margin: 0 auto">
        <el-form-item label="性别">
          <el-select v-model="form.gender" placeholder="请选择性别" style="width: 100%">
            <el-option
                v-for="item in gender"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.realName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="单位">
          <el-select v-model="form.branchId" placeholder="请选择性别" style="width: 100%">
            <el-option
                v-for="item in branchList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="currentShiftHistoryDialogData.realName + '   值班记录'" :visible.sync="shiftHistoryDialog"
               width="40%" :close-on-click-modal="false">
      <el-table :data="shiftHistoryData" border stripe :header-cell-class-name="'headerBg'">

        <el-table-column prop="date" label="日期" width="250" align="center"></el-table-column>
        <el-table-column prop="dayTypeName" label="日期标签" width="150" align="center"></el-table-column>
        <el-table-column  label="班次" width="150" align="center">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.shiftId === 1">上午班</el-tag>
            <el-tag type="primary" v-if="scope.row.shiftId === 2">下午班</el-tag>
            <el-tag type="danger" v-if="scope.row.shiftId === 3">夜班</el-tag>

          </template>
        </el-table-column>

      </el-table>
      <div style="padding: 10px 0">
        <el-pagination
            background
            @size-change="shiftHistoryPageSizeChange"
            @current-change="shiftHistoryPageNumChange"
            :current-page="shiftHistoryPageNum"
            :page-sizes="[2, 5, 10, 20]"
            :page-size="shiftHistoryPageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="shiftHistoryTotalCount">
        </el-pagination>
      </div>
    </el-dialog>

    <el-dialog :title="currentStatisticsDialogData.realName + '   值班统计'" :visible.sync="statisticsDialog" width="40%"
               :close-on-click-modal="false">
      <el-descriptions class="margin-top" title="带边框列表" direction="vertical" :column="7" size="medium" border>
        <el-descriptions-item v-for="(item,index) in statisticsData" :key="index">
          <template slot="label">
            <i class="el-icon-user"></i>
            {{item.dayTypeName}}
          </template>
          <div style="display: flex;align-items: center;justify-content: space-around">
            <div style="display: flex;justify-content: center">
              <el-tag  size="big" :type="item.count === 0 ? 'danger' : 'primary'">{{item.count}}次
                <el-button v-if="item.count !== 0" type="primary" size="mini" class="ml-5" @click="personDayTypeHistoryDialogOpenHistory(item)" icon="el-icon-search" circle></el-button>
              </el-tag>
            </div>
          </div>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <el-dialog :title="currentStatisticsDialogData.realName + '   ' + currentSelectedDayTypeHistoryDialogData.dayTypeName " :visible.sync="isShowPersonDayTypeShiftHistory" width="40%"
               :close-on-click-modal="false">
      <div v-if="personDayTypeShiftHistoryList" v-for="(item,index) in personDayTypeShiftHistoryList" :key="index"
      style="background-color: rgba(233,233,233,0.7);display: flex;flex-wrap: wrap;padding: 2px 4px;border-radius: 5px;margin-bottom: 5px"
      >
        {{item.shiftDate}}---{{item.shiftId === 1 ? '上午班' : item.shiftId === 2 ? '下午班' : '夜班'}}
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Person",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      realName: "",
      phone: '',
      form: {},
      dialogFormVisible: false,
      multipleSelection: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      uploadHeaders: {"token": localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")).token : ''},
      gender: [
        {
          value: 1,
          label: '男'
        }, {
          value: 2,
          label: '女'
        }
      ],
      branchList: [],
      shiftHistoryDialog: false,
      currentShiftHistoryDialogData: [],
      statisticsData: [],
      currentStatisticsDialogData: [],
      statisticsDialog: false,
      shiftHistoryPageNum: 1,
      shiftHistoryPageSize: 10,
      shiftHistoryData: [],
      shiftHistoryTotalCount: 0,
      isShowPersonDayTypeShiftHistory:false,
      personDayTypeShiftHistoryList:[],
      currentSelectedDayTypeHistoryDialogData:[]
    }
  },
  created() {
    this.load()
  },
  methods: {
    personDayTypeHistoryDialogOpenHistory(item){
      this.currentSelectedDayTypeHistoryDialogData = item
      this.isShowPersonDayTypeShiftHistory = true
      this.request.get("/schedule/getPersonDayTypeShiftHistoryList",{
        params:{
          dayTypeId:item.id,
          personId:this.currentStatisticsDialogData.id
        }
      }).then(res => {
        this.personDayTypeShiftHistoryList = res.data

      })
    },
    compare(attr, rev){
      rev = (rev || typeof rev === 'undefined') ? 1 : -1;
      return (a, b) => {
        a = a[attr];
        b = b[attr];
        if (a < b) { return rev * -1; }
        if (a > b) { return rev * 1; }
        return 0;
      };
    },
    getPersonStatistics(){
      this.request("/schedule/getPersonStatistics/" + this.currentStatisticsDialogData.id).then(res => {
        this.statisticsData = res.data.sort(this.compare("id",true))
      })
    },
    getPersonShiftHistory() {
      this.request.get("/schedule/getPersonShiftHistoryList", {
        params: {
          pageNum: this.shiftHistoryPageNum,
          pageSize: this.shiftHistoryPageSize,
          personId: this.currentShiftHistoryDialogData.id
        }
      }).then(res => {
        this.shiftHistoryData = res.data.records
        this.shiftHistoryTotalCount = res.data.total
      })
    },
    statisticsDialogOpenHandler(item) {
      this.currentStatisticsDialogData = JSON.parse(JSON.stringify(item))
      this.getPersonStatistics()
      this.statisticsDialog = true

    },
    shiftHistoryDialogOpenHandler(item) {
      this.currentShiftHistoryDialogData = JSON.parse(JSON.stringify(item))
      this.getPersonShiftHistory()
      this.shiftHistoryDialog = true

    },
    getBranchList() {
      this.request.get("/branch").then(res => {
        this.branchList = res.data
      })
    },
    personStatusChangeHandler(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.request.post("/person", this.form).then(res => {
        if (res.code === '0') {
          this.$message.success("修改成功")
          this.load()
        } else {
          this.$message.error("修改失败")
        }
      })
    },
    load() {
      this.request.get("/person/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          phone: this.phone,
          realName: this.realName
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    reset() {
      this.pageNum = 1
      this.pageSize = 10
      this.phone = ""
      this.realName = ''
      this.load()
    },
    save() {
      this.request.post("/person", this.form).then(res => {
        if (res.code === '0') {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
      this.$nextTick(() => {
        if (this.$refs.img) {
          this.$refs.img.clearFiles();
        }
        if (this.$refs.file) {
          this.$refs.file.clearFiles();
        }
      })
    },
    handleEdit(row) {
      this.getBranchList()
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
      this.$nextTick(() => {
        if (this.$refs.img) {
          this.$refs.img.clearFiles();
        }
        if (this.$refs.file) {
          this.$refs.file.clearFiles();
        }
      })
    },
    handleFileUploadSuccess(res) {
      this.form.file = res
    },
    handleImgUploadSuccess(res) {
      this.form.img = res
    },
    del(id) {
      this.request.delete("/person/" + id).then(res => {
        if (res.code === '0') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      if (!this.multipleSelection.length) {
        this.$message.error("请选择需要删除的数据")
        return
      }
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/person/del/batch", ids).then(res => {
        if (res.code === '0') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    shiftHistoryPageNumChange(pageNum) {
      this.shiftHistoryPageNum= pageNum
      this.getPersonShiftHistory()
    },
    shiftHistoryPageSizeChange(pageSize) {
      this.shiftHistoryPageSize = pageSize
      this.getPersonShiftHistory()

    },
    download(url) {
      window.open(url)
    },
    query() {
      this.pageNum = 1
      this.load()
    },
  },

}

</script>


<style>
.headerBg {
  background: #eee !important;
}
</style>
